/* vim:ft=objc:
 * $Id: ConfigController.m 148 2005-05-29 20:15:44Z bmoore $
 *
 * Copyright (c) 2004, 2005  Branden J. Moore.
 *
 * This file is part of MacBiff, and is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * MacBiff is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with MacBiff; if not, write to the Free Software Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA  02111-1307 USA.
 *
 */

#define GENERAL_PREFS_LABEL @"General"
#define GENERAL_PREFS_TIP @"General Configuration Options"
#define ACCOUNTS_PREFS_LABEL @"Accounts"
#define ACCOUNTS_PREFS_TIP @"Configure Accounts"
#define PLUGINS_PREFS_LABEL @"PlugIns"
#define PLUGINS_PREFS_TIP @"Configure PlugIns"

#import "ConfigController.h"
#import <MacBiffPlugIn/PreferencesController.h>
#import <MacBiffPlugIn/debug.h>
#import <MacBiffPlugIn/plugins.h>
#import <MacBiffPlugIn/PlugIn.h>
#import <MacBiffPlugIn/Account.h>
#import <MacBiffPlugIn/Action.h>
#import <MacBiffPlugIn/Folder.h>


extern NSMutableDictionary *accountBundles;
extern NSMutableDictionary *actionBundles;
extern NSMenu* gMainMenu;
extern NSLock* gMenuLock;

ConfigController *configController = Nil;

@implementation ConfigController

- (id) init
{
	self =  [super init];
	configController = self;
	return self;
}


- (void) setAccountList: (NSMutableArray*) list
{
	if ( accountList ) [accountList release];
	accountList = list;
	[accountList retain];
}


- (IBAction)noOp:(id)sender
{
}


- (IBAction)configure:(id)sender
{
	[configWindow makeKeyAndOrderFront: Nil];
	[NSApp activateIgnoringOtherApps: YES];
}


- (void) configureAccount: (id)sender
{
	NSMenuItem *mi = (NSMenuItem*)sender;
	int idx;
	idx = [accountList indexOfObject: [mi representedObject]];
	if ( idx < 0 ) return;

	[self configure: Nil];
	[self setView: accountView];
	[accountListTableView selectRow: idx byExtendingSelection: NO];
	[self accountSelectAccount: Nil];
}


- (void) awakeFromNib
{
	NSString *name;
	NSToolbarItem *item;

	tbItems = [[NSMutableDictionary alloc] init];

	name = [[NSString alloc] initWithString: ACCOUNTS_PREFS_LABEL];
	item = [[NSToolbarItem alloc] initWithItemIdentifier: name];
	[item setPaletteLabel: name];
	[item setLabel: name];
	[item setImage: [NSImage imageNamed: @"AccountPreferences"]];
	[item setToolTip: [NSString stringWithString: ACCOUNTS_PREFS_TIP]];
	[item setTarget: self];
	[item setAction: @selector(toolbarItemClicked:)];
	[tbItems setObject: item forKey: name];
	[item release];
	[name release];

	name = [[NSString alloc] initWithString: GENERAL_PREFS_LABEL];
	item = [[NSToolbarItem alloc] initWithItemIdentifier: name];
	[item setPaletteLabel: name];
	[item setLabel: name];
	[item setImage: [NSImage imageNamed: @"GeneralPreferences"]];
	[item setToolTip: [NSString stringWithString: GENERAL_PREFS_TIP]];
	[item setTarget: self];
	[item setAction: @selector(toolbarItemClicked:)];
	[tbItems setObject: item forKey: name];
	[item release];
	[name release];

	name = [[NSString alloc] initWithString: PLUGINS_PREFS_LABEL];
	item = [[NSToolbarItem alloc] initWithItemIdentifier: name];
	[item setPaletteLabel: name];
	[item setLabel: name];
	[item setImage: [NSImage imageNamed: @"plugin"]];
	[item setToolTip: [NSString stringWithString: PLUGINS_PREFS_TIP]];
	[item setTarget: self];
	[item setAction: @selector(toolbarItemClicked:)];
	[tbItems setObject: item forKey: name];
	[item release];
	[name release];


	toolbar = [[NSToolbar alloc] initWithIdentifier: @"ConfigBar"];
	[toolbar setDelegate: self];
	[toolbar setAllowsUserCustomization: YES];
	[toolbar setAutosavesConfiguration: YES];

	[configWindow setToolbar: toolbar];

	[self setView: generalView];
}


- (void) dealloc
{
	[toolbar release];
	[tbItems release];
	if ( accountList ) [accountList release];
	[super dealloc];
}


/* Account Work */
- (IBAction)accountAddAccount:(id)sender
{
	[newAccountNameTextField setStringValue: @""];
	/* I shouldn't do this, but I want IMAP to be default,
	 * no matter what order they might be in */
	[newAccountTypePopUp selectItemWithTitle: @"IMAP"];
	[newAccountTypePopUp synchronizeTitleAndSelectedItem];

	[NSApp beginSheet: newAccountSheet
	   modalForWindow: configWindow
	    modalDelegate: self
	   didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
	      contextInfo: Nil];
}


- (IBAction)accountDelAccount:(id)sender
{
	int res = 0;
	if ( !currentAccountInfo ) return;

	NSAlert *alert = [NSAlert alertWithMessageText: @"Confirm Delete"
					 defaultButton: @"No"
				       alternateButton: @"Yes"
					   otherButton: Nil
			     informativeTextWithFormat:
			     @"Are you sure that you wish to remove "
			     "the account '%@'?", [currentAccountInfo name]];
	res = [alert runModal];

	if ( res ) {
		return;
	} else {
		[currentAccountInfo deactivate: Nil];
		[prefs removeAccount: [currentAccountInfo name]];
		[prefs commitAccounts];
		[accountList removeObject: currentAccountInfo];
		[accountListTableView deselectAll: Nil];
		[accountListTableView reloadData];
		[self accountSelectAccount: Nil];
	}
}


- (IBAction)accountSetCheckInterval:(id)sender
{
	int newval;
	newval = [accountCheckIntervalTextField intValue];
	[accountCheckIntervalStepper setIntValue: newval];
	[[prefs account: [[currentAccountInfo account] name]]
		setObject: [NSNumber numberWithInt: newval]
		forKey: @"Check Interval"];
	[prefs commitAccounts];
}


- (IBAction)accountSetDefaultPlugins:(id)sender
{
	BOOL use_default = [accountDefaultPluginButton state] == NSOnState;
	/* Damnit Apple... This should work. */
	[accountPluginTableView setEnabled: !use_default];

	[[prefs account: [[currentAccountInfo account] name]]
		setObject: [NSNumber numberWithBool: use_default]
		forKey: @"Use Default Actions"];
	[prefs commitAccounts];
}


- (IBAction)accountSetName:(id)sender
{
	dprintf("Changing Account Name from '%s' to '%s'\n",
			[[currentAccountInfo name] cString],
			[[accountNameTextField stringValue] cString]);
	[currentAccountInfo reName: [accountNameTextField stringValue]];
	[prefs commitAccounts];
	[accountListTableView reloadData];
}


- (void) accountSetTypeView: (Account*) acct
{
	NSArray *subviews = [accountTypeView subviews];
	while ( [subviews count] ) {
		[[subviews objectAtIndex: 0] removeFromSuperview];
	}
	[acct setupView];
	[accountTypeView addSubview: [acct configView]];
}


- (IBAction)accountSetType:(id)sender
{
#if 0	/* BJM:  Needs to do more than this... Change the object itself */
	NSString *str = [[accountTypePopUp selectedTitle] copy];
	[[prefs account: [currentAccountInfo name]]
		setObject: str
		forKey: @"Type"];
	[str release];
	[prefs commitAccounts];
	[accountSetTypeView: [currentAccountInfo account]];
#endif
}


- (IBAction)accountStepCheckInterval:(id)sender
{
	int newval;
	newval = [accountCheckIntervalStepper intValue];
	[accountCheckIntervalTextField setIntValue: newval];
	[[prefs account: [[currentAccountInfo account] name]]
		setObject: [NSNumber numberWithInt: newval]
		forKey: @"Check Interval"];
	[prefs commitAccounts];
}


- (IBAction)accountSelectAccount:(id)sender
{
	int idx = [accountListTableView selectedRow];
	Account *acct = Nil;
	NSDictionary *acctPrefs = Nil;

	if ( idx < 0 ) {
		currentAccountInfo = Nil;
		[accountTabView setHidden: YES];
	} else {
		currentAccountInfo = [accountList objectAtIndex: idx];
		acct = [currentAccountInfo account];
		acctPrefs = [prefs account: [acct name]];
		dprintf("Loading index %d ('%s')\n", idx,[[acct name] cString]);

		[accountTabView selectTabViewItemAtIndex: 0];
		[accountNameTextField setStringValue: [acct name]];
		[accountTypePopUp selectItemWithTitle: [[acct class] plugInName]];
		[accountTypePopUp synchronizeTitleAndSelectedItem];
		[accountTypePopUp setEnabled: NO];

		[self accountSetTypeView: acct];

		BOOL use_default = [[acctPrefs objectForKey:
				@"Use Default Actions"] boolValue];
		[accountDefaultPluginButton setState: use_default ?
			NSOnState : NSOffState];
		/* Damnit Apple... This should work. */
		[accountPluginTableView setEnabled: !use_default];

		[accountCheckIntervalTextField setObjectValue:
			[acctPrefs objectForKey: @"Check Interval"]];
		[accountCheckIntervalStepper setObjectValue:
			[acctPrefs objectForKey: @"Check Interval"]];

		[accountTabView setHidden: NO];

		[accountMailboxOutlineView reloadData];
	}
}


- (void)sheetDidEnd: (NSWindow *) sheet
	 returnCode: (int) returnCode
	contextInfo: (void *) contextInfo
{
	Class acctClass;
	Account *account = Nil;
	AccountInfo *info = Nil;
	NSString *type = Nil;
	NSString *name = Nil;

	[sheet orderOut: self];

	if ( !returnCode ) return; /* Use Canceled */

	name = [[newAccountNameTextField stringValue] copy];
	type = [[newAccountTypePopUp titleOfSelectedItem] copy];
	dprintf("Chose name: '%s'  Type: '%s'\n",
			[name cString], [type cString]);

	if ( [prefs account: name]  != Nil ) {
		NSAlert *alert = [NSAlert alertWithMessageText:
			@"Already Exists"
			defaultButton: @"OK"
			alternateButton: Nil
			otherButton: Nil
			informativeTextWithFormat:
				@"An Account named '%@' already exists.",
				name];
		[alert runModal];
		[self accountAddAccount: Nil];
		[name release];
		[type release];
		return;
	}

	[prefs addAccount: name withType: type];
	[prefs commitAccounts];
	acctClass = [[accountBundles objectForKey: type] principalClass];
	account = [[acctClass alloc] initWithName: name];
	info = [[AccountInfo alloc] initWithAccount: account
			andMenu: gMainMenu andLock: gMenuLock];
	[info activate: Nil];

	[accountList addObject: info];
	[accountListTableView reloadData];
	[accountListTableView selectRow: [accountList count] -1
		   byExtendingSelection: NO];
	[self accountSelectAccount: Nil];

	[name release];
	[type release];
	[account release];
	[info release];
}


- (IBAction)newAccountDismiss:(id)sender
{
	[NSApp endSheet: newAccountSheet returnCode: 0];
}


- (IBAction)newAccountOK:(id)sender
{
	[NSApp endSheet: newAccountSheet returnCode: 1];
}




/* General Preferences */
- (IBAction)generalChooseMailApp:(id)sender
{
	int res;
	NSArray *ft = [NSArray arrayWithObject: @"app"];
	NSOpenPanel *op = [NSOpenPanel openPanel];

	[op setAllowsMultipleSelection: NO];

	res = [op runModalForDirectory: nil
		file: [generalMailAppTextField stringValue]
		types: ft];
	if ( res == NSOKButton ) {
		NSArray *files = [op filenames];
		NSString *str = [[files objectAtIndex: 0] copy];
		[generalMailAppTextField setStringValue: str];
		[[prefs misc] setObject: str forKey: @"Mail App"];
		[str release];
		[prefs commitMisc];
	}
}


- (IBAction)generalSetChangeColor:(id)sender
{
	[[prefs display] setObject: [NSNumber numberWithBool:
			([generalChangeColorButton state] == NSOnState)]
		forKey: @"Change Color"];
	[prefs commitDisplay];
}


- (IBAction)generalSetMailApp:(id)sender
{
	NSString *str = [[generalMailAppTextField stringValue] copy];
	[[prefs misc] setObject: str forKey: @"Mail App"];
	[str release];
	[prefs commitMisc];
}


- (IBAction)generalSetShowActivityOnCheck:(id)sender
{
	[[prefs misc] setObject: [NSNumber numberWithBool:
			([generalShowActivityButton state] == NSOnState)]
		forKey: @"Show Activity On Check"];
	[prefs commitMisc];
}


- (IBAction)generalSetShowIcon:(id)sender
{
	[[prefs display] setObject: [NSNumber numberWithBool:
			([generalShowIconButton state] == NSOnState)]
		forKey: @"Show Icon"];
	[prefs commitDisplay];
}


- (IBAction)generalSetShowText:(id)sender
{
	[[prefs display] setObject: [NSNumber numberWithBool:
			([generalShowTextButton state] == NSOnState)]
		forKey: @"Show Text"];
	[prefs commitDisplay];
}


- (IBAction)generalSetShowTotal:(id)sender
{
	[[prefs display] setObject: [NSNumber numberWithBool:
			([generalShowTotalButton state] == NSOnState)]
		forKey: @"Show Total"];
	[prefs commitDisplay];
}


/* Plugin Work */
- (IBAction)pluginSelectPlugIn:(id)sender
{
	int row = [pluginTableView selectedRow];
	if ( row < 0 ) {
		/* Nothing Selected */
		[pluginAuthorTextField setStringValue: @""];
		[pluginNameTextField setStringValue: @""];
		[pluginTypeTextField setStringValue: @""];
		[pluginURLTextField setStringValue: @"http://www.forkit.org/macbiff/"];
		[pluginVersionTextField setStringValue: @""];
		[pluginConfigureView setHidden: YES];
		while ( [[pluginConfigureView subviews] count] > 1 ) {
			dprintf("Removing a subview. \n");
			[[[pluginConfigureView subviews] objectAtIndex: 1]
				removeFromSuperview];
		}
	} else {
		NSMutableArray *bundles = [NSMutableArray arrayWithCapacity: 5];
		[bundles addObjectsFromArray: [actionBundles allValues]];
		[bundles addObjectsFromArray: [accountBundles allValues]];
		Class plugin = [[bundles objectAtIndex: row] principalClass];

		[pluginAuthorTextField setStringValue: [plugin plugInAuthor]];
		[pluginNameTextField setStringValue: [plugin plugInName]];
		[pluginTypeTextField setStringValue: [plugin plugInType]];
		[pluginURLTextField setStringValue: [plugin plugInUrl]];
		[pluginVersionTextField setStringValue: [plugin plugInVersion]];

		if ( [[plugin plugInType] isEqual: ACTION_PLUGIN_TYPE] ) {
			PlugIn *plug = [actionPlugIns objectForKey:
				[plugin plugInName]];
			[pluginConfigureView setHidden: NO];
			/* Remove the old view, if there was one */
			while ( [[pluginConfigureView subviews] count] ) {
				dprintf("Removing a subview. \n");
				[[[pluginConfigureView subviews] objectAtIndex:0]
					removeFromSuperview];
			}
			[pluginConfigureView addSubview: [plug configView]];
			[plug setupView];
		} else {
			[pluginConfigureView setHidden: YES];
		}
	}
}

- (IBAction)pluginGoURL:(id)sender
{
	[[NSWorkspace sharedWorkspace]
		openURL: [NSURL URLWithString:
			[pluginURLTextField stringValue]]];

}

/*
 * Data Source Methods
 */

/* Outline View:  Used for Folder Lists */

- (int) outlineView: (NSOutlineView*) ov
	numberOfChildrenOfItem: (id) item
{
	NSArray *arr = (item == Nil) ?
		[[currentAccountInfo account] allFolders] :
		[item subFolders];
	return [arr count];
}


- (BOOL) outlineView: (NSOutlineView*) ov
	isItemExpandable: (id) item
{
	NSArray *arr = (item == Nil) ?
		[[currentAccountInfo account] allFolders] :
		[item subFolders];
	return [arr count];
}


- (id) outlineView: (NSOutlineView*) ov
	child: (int) idx
	ofItem: (id) item
{
	NSArray *arr = (item == Nil) ?
		[[currentAccountInfo account] allFolders] :
		[item subFolders];
	return [arr objectAtIndex: idx];
}


- (id) outlineView: (NSOutlineView*) ov
	objectValueForTableColumn: (NSTableColumn*) tableColumn
	byItem: (id) item
{
	if ( item == Nil ) return Nil;

	NSString *ident = [tableColumn identifier];
	Folder *folder = (Folder*)item;
	id result = Nil;

	if ( [ident isEqual: @"Folder"] ) {
		result = [folder name];
	} else if ( [ident isEqual: @"Masked"] ) {
		result = [NSNumber numberWithBool: [folder masked]];
	} else if ( [ident isEqual: @"Disabled"] ) {
		result = [NSNumber numberWithBool: [folder disabled]];
	}

	return result;
}


- (void) outlineView: (NSOutlineView *) ov
	setObjectValue: (id) object
	forTableColumn: (NSTableColumn*) tableColumn
	byItem: (id) item
{
	if ( item == Nil ) return;

	NSString *ident = [tableColumn identifier];
	Folder *folder = (Folder*)item;

	if ( [ident isEqual: @"Folder"] ) {
		return;
	} else if ( [ident isEqual: @"Masked"] ) {
		[folder setMasked: [object boolValue] recursively: YES];
	} else if ( [ident isEqual: @"Disabled"] ) {
		[folder setDisabled: [object boolValue] recursively: YES];
	}

	[ov reloadItem: item reloadChildren: YES];
}


/* tableView:
 * Used for Plugin Lists and Account List
 */

- (int) numberOfRowsInTableView: (NSTableView*) tv
{
	if ( tv == generalDefaultPluginTableView ) {
		return [actionBundles count];
	} else if ( tv == pluginTableView ) {
		return [actionBundles count] + [accountBundles count];
	} else if ( tv == accountPluginTableView ) {
		return [actionBundles count];
	} else if ( tv == accountListTableView ) {
		return [accountList count];
	} else {
		return 0;
	}
}


- (id) tableView: (NSTableView*) tv
	objectValueForTableColumn: (NSTableColumn*) tableColumn
	row: (int) row
{
	NSString *ident = [tableColumn identifier];

	if ( tv == generalDefaultPluginTableView ) {
		NSArray *names = [actionPlugIns allKeys];
		NSString *name = [names objectAtIndex: row];
		if ( [ident isEqual: @"name"] ) {
			return name;
		} else if ( [ident isEqual: @"precheck"] ) {
			return [NSNumber numberWithBool:
				[[prefs defaultPreCheckActions]
					containsObject: name]];
		} else if ( [ident isEqual: @"postcheck"] ) {
			return [NSNumber numberWithBool:
				[[prefs defaultPostCheckActions]
					containsObject: name]];
		} else {
			return nil;
		}
	} else if ( tv == pluginTableView ) {
		NSMutableArray *bundles = [NSMutableArray arrayWithCapacity: 5];
		[bundles addObjectsFromArray: [actionBundles allValues]];
		[bundles addObjectsFromArray: [accountBundles allValues]];
		Class plugin = [[bundles objectAtIndex: row] principalClass];

		if ( [ident isEqual: @"name"] ) {
			return [plugin plugInName];
		} else if ( [ident isEqual: @"version"] ) {
			return [plugin plugInVersion];
		} else if ( [ident isEqual: @"type"] ) {
			return [plugin plugInType];
		} else {
			return nil;
		}
	} else if ( tv == accountPluginTableView ) {
		NSDictionary *acctPrefs = [prefs account: [[currentAccountInfo account] name]];
		NSArray *preCheck = [acctPrefs objectForKey: @"Pre-Check Actions"];
		NSArray *postCheck = [acctPrefs objectForKey: @"Post-Check Actions"];
		NSArray *names = [actionPlugIns allKeys];
		NSString *name = [names objectAtIndex: row];

		if ( [ident isEqual: @"name"] ) {
			return name;
		} else if ( [ident isEqual: @"precheck"] ) {
			return [NSNumber numberWithBool:
				[preCheck containsObject: name]];
		} else if ( [ident isEqual: @"postcheck"] ) {
			return [NSNumber numberWithBool:
				[postCheck containsObject: name]];
		} else {
			return nil;
		}
	} else if ( tv == accountListTableView) {
		AccountInfo *acct = [accountList objectAtIndex: row];
		if ( [ident isEqual: @"name"] ) {
			return [acct name];
		} else if ( [ident isEqual: @"enabled"] ) {
			return [NSNumber numberWithBool: [acct enabled]];
		} else {
			return nil;
		}
	} else {
		return nil;
	}
}


- (void) tableView: (NSTableView*) tv
	setObjectValue: (id) object
	forTableColumn: (NSTableColumn*) tableColumn
	row: (int) row
{
	NSString *ident = [tableColumn identifier];

	if ( tv == generalDefaultPluginTableView ) {
		NSArray *names = [actionPlugIns allKeys];
		NSString *name = [[[names objectAtIndex: row] copy]autorelease];
		BOOL enable = [object boolValue];
		if ( [ident isEqual: @"name"] ) {
			;	/* Can't edit this column */
		} else if ( [ident isEqual: @"precheck"] ) {
			NSMutableArray *list = [[[prefs defaultPreCheckActions]
				mutableCopy] autorelease];
			int idx = [list indexOfObject: name];

			if ( enable && (idx == NSNotFound) ) {
				[list addObject: name];
			} else if ( !enable && (idx != NSNotFound) ) {
				[list removeObject: name];
			}

			[prefs setDefaultPreCheckActions: list];
		} else if ( [ident isEqual: @"postcheck"] ) {
			NSMutableArray *list = [[[prefs defaultPostCheckActions]
				mutableCopy] autorelease];
			int idx = [list indexOfObject: name];

			if ( enable && (idx == NSNotFound) ) {
				[list addObject: name];
			} else if ( !enable && (idx != NSNotFound) ) {
				[list removeObject: name];
			}

			[prefs setDefaultPostCheckActions: list];
		}
		[prefs commit];
		return;
	} else if ( tv == pluginTableView ) {
		/* We don't allow editing in this table */
		return;
	} else if ( tv == accountPluginTableView ) {
		NSArray *names = [actionPlugIns allKeys];
		NSString *name = [[[names objectAtIndex: row] copy]autorelease];
		BOOL enable = [object boolValue];
		NSMutableDictionary *acctDict =
			[prefs account: [currentAccountInfo name]];
		if ( [ident isEqual: @"name"] ) {
			;	/* Can't edit this column */
		} else if ( [ident isEqual: @"precheck"] ) {
			NSMutableArray *list = [acctDict objectForKey:
							@"Pre-Check Actions"];
			int idx = [list indexOfObject: name];

			if ( enable && (idx == NSNotFound) ) {
				[list addObject: name];
			} else if ( !enable && (idx != NSNotFound) ) {
				[list removeObject: name];
			}

		} else if ( [ident isEqual: @"postcheck"] ) {
			NSMutableArray *list = [acctDict objectForKey:
							@"Post-Check Actions"];
			int idx = [list indexOfObject: name];

			if ( enable && (idx == NSNotFound) ) {
				[list addObject: name];
			} else if ( !enable && (idx != NSNotFound) ) {
				[list removeObject: name];
			}

		}
		[prefs commitAccounts];
		return;
	} else if ( tv == accountListTableView) {
		if ( row > (int)[accountList count] ) return;
		AccountInfo *acct = [accountList objectAtIndex: row];
		if ( [ident isEqual: @"name"] ) {
			[[acct account] setName: object];
		} else if ( [ident isEqual: @"enabled"] ) {
			[[acct account] setEnabled: [object boolValue]];
		}
	}
}


/* Toolbar Datasource */
- (NSToolbarItem*) toolbar: (NSToolbar*) tb
	itemForItemIdentifier: (NSString*) itemIdentifier
	willBeInsertedIntoToolbar: (BOOL) flag
{
	return [tbItems objectForKey: itemIdentifier];
}


- (NSArray*) toolbarDefaultItemIdentifiers: (NSToolbar*) tb
{
	return [tbItems allKeys];
}


- (NSArray*) toolbarAllowedItemIdentifiers: (NSToolbar*) tb
{
	return [tbItems allKeys];
}


/* User Actions for Toolbar */
- (void) toolbarItemClicked: (NSToolbarItem*) item
{
	NSView *view = Nil;

	if ( [[item label] isEqual: GENERAL_PREFS_LABEL] ) {
		view = generalView;
	} else if ( [[item label] isEqual: ACCOUNTS_PREFS_LABEL] ) {
		view = accountView;
	} else if ( [[item label] isEqual: PLUGINS_PREFS_LABEL] ) {
		view = pluginView;
	} else {
		alert("Invalid Item!\n");
		return;
	}

	[self setView: view];
}


- (void) setView: (NSView*) view
{
	if ( view == currentView ) return;

	NSRect windowFrame, newWindowFrame;
	unsigned int toolbarHeight, newWindowHeight;

	windowFrame = [NSWindow contentRectForFrameRect: [configWindow frame]
	       styleMask: [configWindow styleMask]];

	toolbarHeight = [toolbar isVisible] ? NSHeight(windowFrame) -
		NSHeight([[configWindow contentView] frame]) : 0;

	newWindowHeight = NSHeight( [view frame] ) + toolbarHeight;

	newWindowFrame = [NSWindow frameRectForContentRect:
		NSMakeRect( NSMinX( windowFrame ),
				NSMaxY( windowFrame ) - newWindowHeight,
				NSWidth( windowFrame ),
				newWindowHeight )
		styleMask: [configWindow styleMask]];


	[configWindow setContentView: view];

	[configWindow setFrame: newWindowFrame
		       display: YES
		       animate: [configWindow isVisible]];

	[configWindow display];
	[configWindow setInitialFirstResponder: view];
	[configWindow makeFirstResponder: view];

	if ( view == generalView ) {
		[self setViewToGeneral];
	} else if ( view == accountView ) {
		[self setViewToAccounts];
	} else if ( view == pluginView ) {
		[self setViewToPlugIns];
	} else {
		/* Punt, I don't know what this is */
	}
	currentView = view;
	[currentView becomeFirstResponder];
}


- (void) setViewToGeneral
{
	[generalChangeColorButton setState:
		([[[prefs display] objectForKey: @"Change Color"] boolValue]) ?
			 NSOnState : NSOffState];
	[generalShowIconButton setState:
		([[[prefs display] objectForKey: @"Show Icon"] boolValue]) ?
			 NSOnState : NSOffState];
	[generalShowTextButton setState:
		([[[prefs display] objectForKey: @"Show Text"] boolValue]) ?
			 NSOnState : NSOffState];
	[generalShowTotalButton setState:
		([[[prefs display] objectForKey: @"Show Total"] boolValue]) ?
			 NSOnState : NSOffState];

	[generalShowActivityButton setState:
		([[[prefs misc] objectForKey: @"Show Activity On Check"] boolValue]) ?
			 NSOnState : NSOffState];

	[generalMailAppTextField setStringValue:
			[[prefs misc] objectForKey: @"Mail App"]];
}


- (void) setViewToAccounts
{
	if ( currentAccountInfo == Nil ) {
		[accountListTableView selectRowIndexes:
				[NSIndexSet indexSetWithIndex: 0]
			byExtendingSelection: NO];
	}

	[newAccountTypePopUp removeAllItems];
	[newAccountTypePopUp addItemsWithTitles: [accountBundles allKeys]];

	[accountTypePopUp removeAllItems];
	[accountTypePopUp addItemsWithTitles: [accountBundles allKeys]];
	/* Bizzare hack to take care of popup button being visible */
	[accountTypePopUp setHidden: YES];
	[accountTypePopUp setHidden: NO];

	[accountTabView setHidden: YES];

	[self accountSelectAccount: Nil];
}


- (void) setViewToPlugIns
{
}

@end
